<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class SiteController extends Controller
{

    public $type = "";

    public function actions()
    {
        return array(
            'captcha'=>array(
                'class'=>'CCaptchaAction',
                'backColor'=>0xFFFFFF,
        ),
            'page'=>array(
                'class'=>'CViewAction',
        ),
        );
    }

    public function actionIndex()
    {
                if(Yii::app()->params['app']['initialized']==false){
            $this->redirect(Yii::app()->createUrl("install/index"));
            return;
        }else{            if(User::model()->count()==0){
                $this->redirect(Yii::app()->createUrl("install/setup3"));
                return false;
            }
        }
        $this->layout='//layouts/site';
        $this->redirect(array("site/login"));
    }
    public function actionScreens()
    {
        $this->layout='//layouts/site';
        $this->render('screens');
    }
    public function actionDownload()
    {
        $this->render('download');
    }
    public function actionRegister()
    {
        $this->type = "注册";
        $this->layout='//layouts/login';
        $model = new RegisterForm();
        if(Yii::app()->request->isPostRequest){
            $model->attributes=$_POST['RegisterForm'];
            if ($model->save()){
                if (SYSTEM_STATUS == "init") {
                    SystemManager::addUserSuccess();
                }
                $form = new LoginForm;
                $form->userName = $model->name;
                $form->password = $model->password;
                if ($form->login()) {
                    $this->redirect(Yii::app()->user->returnUrl);
                } else {
                    $this->redirect(array("site/login"));
                }
            }
        }
        $this->render('register',array(
         "model"=>$model
        ));
    }
    public function actionForgetpasswd()
    {
        $this->type = "找回密码";
        $this->layout='//layouts/login';
        $model = new ForgetpasswdForm();
        if(Yii::app()->request->isPostRequest){
            $model->attributes=$_POST['ForgetpasswdForm'];
            $model->save();
        }
        $this->render('forgetpasswd',array(
         "model"=>$model
        ));
    }
    public function actionModifypassword()
    {
        $this->layout='//layouts/login';
        $model = new ModifypasswdForm();
        if(Yii::app()->request->isPostRequest){
            $model->attributes=$_POST['ModifypasswdForm'];
            $model->save();
            
            if (SYSTEM_STATUS == "init" && $model->resetPassSuccess) {
                SystemManager::addUserSuccess();
            }elseif (SYSTEM_STATUS == "done" && $model->password == "admin" && $model->resetPassSuccess) {
                SystemManager::delUserSuccess();
            }
        }
        $this->render('modifypasswd',array(
         "model"=>$model
        ));
    }
    public function actionLogin()
    {
        $this->type = "登录";
        $model=new LoginForm;
        if(isset($_POST['LoginForm']))
        {
            $model->attributes=$_POST['LoginForm'];
            if($model->login()){
                if (Yii::app()->session["redirect"] && Yii::app()->session['user']['isAdmin']) {
                    $url = Yii::app()->session["redirect"];
                    $this->redirect($url);
                    unset($_SESSION["redirect"]);
                }
                $this->redirect(Yii::app()->user->returnUrl);
            }
        }
        $this->render('login',array('model'=>$model));
    }
    
    public function actionError()
    {
        if($error=Yii::app()->errorHandler->error)
        {
            if(Yii::app()->request->isAjaxRequest)
            echo $error['message'];
            else
            $this->render('error', $error);
        }
    }

    
    public function actionContact()
    {
        $model=new ContactForm;
        if(isset($_POST['ContactForm']))
        {
            $model->attributes=$_POST['ContactForm'];
            if($model->validate())
            {
                $headers="From: {$model->email}\r\nReply-To: {$model->email}";
                mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);
                Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
                $this->refresh();
            }
        }
        $this->render('contact',array('model'=>$model));
    }


    
    public function actionLogout()
    {
                $user = Yii::app()->session["user"];
        if(isset($user)){
            $appId=Yii::app()->session["appId"];
            $deviceId=Yii::app()->session["deviceId"];
            $userId = $user["id"];
            $onlineDevice = OnlineDevice::model()->find("user_id=? and application_id=? and device_id=?",array($userId,$appId,$deviceId));
            if(isset($onlineDevice)){
                $onlineDevice->deviceLogout();
            }
        }
                Yii::app()->session["user"] = null;
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->homeUrl);
    }
    
    public function actionLicense() {
        $this->redirect("http://bbs.miniyun.cn/forum.php?mod=viewthread&tid=110");
    }
}